define(['jquery'], function() {
    var adBox = $(".hc-ad");
    var type = 'all',
        duration = 3000,
        isClose = true,
        autoClose = true;

    //loadCSS("../src/propments/css/ad.css", adBox[0]);
    var run = function() {
        adBox.each(function(index, ele) {
            var that = $(this)
            var cur_type = that.attr('type');
            adInit(that); //广告初始化
            adClose(that); //添加关闭按钮，同时绑定点击后，关闭广告图片的事件
            bindClose(that); //绑定自动关闭事件
        });
    }


    //广告初始化
    function adInit(ele) {
        var bg_img = ele.data('bgimg')
        var w, h;

        //设置csS
        buildCss(); //生成CSS


        //是否自动关闭属性的获取
        if (ele.attr('autoClose') != undefined && ele.attr('autoClose') == 'false') {
            autoClose = false;
        }

        //等待时间获取
        if (ele.attr('duration') != undefined) {
            duration = parseInt(ele.attr('duration'))
        }

        //是否显示关闭按钮属性isClose  获取
        if (ele.attr('isClose') != undefined && ele.attr('isClose') == 'false') {
            isClose = false;
        }

        //加载图片
        var img_obj = new Image();
        img_obj.onload = function() {
            w = img_obj.width;
            h = img_obj.height;

            if (ele.attr('width') != undefined) {
                w = parseInt(ele.attr('width'));
            }
            if (ele.attr('height') != undefined) {
                h = parseInt(ele.attr('height'));
            }
            //显示背景图片
            ele.css({
                'background-image': 'url(' + bg_img + ')',
                'width': w + 'px',
                'height': h + 'px',
                'background-size': 'cover',
            })
        }
        img_obj.src = bg_img;



    }

    //生成css 标签
    function buildCss() {
        /* <style>
                .hc-ad {

                }
                .close {
                    
                }
            </style> */
        var style = "<style id='adStyle'>" +
            "        .hc-ad {position: relative;}" +
            "        .close {position: absolute;top: 0;right: 0;cursor: pointer; width: 20px;height: 20px;line-height: 20px;text-align: center;background-color: rgba(255, 255, 255, .6);border-radius: 50%; }" +
            "        .close:hover { background-color: rgba(255, 255, 255, 1);color: red; }" +
            "   </style>";

        if ($("#adStyle").length == 0) {
            adBox.eq(0).before(style);
        }
    }

    //添加关闭按钮   <span class="close">X</span>
    function adClose(el) {
        //1、通过 isClose 来判断是否要显示，如果为true
        //2、创建  span  标签, 加一个class= close
        //3、 将 span 添加到  我们广告容器 adBox
        //4、 绑定click 事件，点击后，关闭这个容器
        if (!isClose) return;
        var span = adBox.html("<span class='close'>X<span>").find(".close").eq(0); //添加span 并且获取这个span
        //console.log(span);
        span.on('click', function() {
            adBox.hide(); //关闭广告
        });
    }


    //绑定自动关闭
    function bindClose(ele) {
        isClose = Boolean(ele.attr('isClose') != undefined ? ele.attr('isClose') : isClose);
        duration = ele.attr('duration') != undefined ? ele.attr('duration') : duration;


        if (autoClose === true) {
            console.log('go');
            setTimeout(function() {
                ele.hide();
            }, duration);
        }
    }

    return {
        run: run,
    }

});